﻿using Cofe.Core;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cofe.Core.Actions
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class CopyDirectoryContent : ITransferHelper
    {
        public TransferMode GetSupportedMode(ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm, ICofeDirectoryInfo too)
        {
            if (entries == null || entries.Length == 0)
                return TransferMode.None;

            foreach (var entry in entries)
                if (!(entry.Is<ICofeDirectoryInfo>()) || !entries.First().CofeAttributes.HasFlag(CofeAttributes.FileSystem))
                return TransferMode.None;
            return TransferMode.Copy;
        }

        public async Task<TransferMode> TransferAsync(TransferMode mode, ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm, 
            ICofeDirectoryInfo too, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            if (mode == TransferMode.Copy && GetSupportedMode(entries, fromm, too) == TransferMode.Copy)
            {
                foreach (var entry in entries)
                {
                    var srcDirectory = entry.As<ICofeDirectoryInfo>();
                    var destDirectory = await too.CreateCofeDirectoryAsync(entry.Name);

                    var srcSubEntries = await srcDirectory.GetCofeSystemInfosAsync(new Filter.IEntryFilter[] { },
                        new Filter.IOptionFilter[] { }, EntryConstructionFlags.Default, EntryConstructor);

                    foreach (var subEntry in srcSubEntries)
                        if (!(cts.IsCancellationRequested))
                            await EntryFE.CopyAsync(subEntry, destDirectory, progress, cts);
                }
                return TransferMode.Copy;
            }
            else throw new NotImplementedException();
        }

        public Task<TransferMode> RenameAsync(TransferMode mode, ICofeSystemInfo entry, ICofeDirectoryInfo fromm,
         string newName, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            throw new NotImplementedException();
        }


        private IEntryConstructor _entryConstructor = null;
        public ITransferHelper[] _allHelpers = null;

        public IEntryConstructor EntryConstructor
        {
            get { return _entryConstructor ?? CofeServices.ServiceLocater.FindService<IEntryConstructor>(); }
            set { _entryConstructor = value; }
        }
        public ITransferHelper[] AllHelpers
        {
            get { return _allHelpers ?? CofeServices.ServiceLocater.FindAllServices<ITransferHelper>().ToArray(); }
            set { _allHelpers = value; }
        }
    }
}
